﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using moose.irc;

namespace MooseIRC.Client.Server {

    /// <summary>
    /// Status/Root IRC Session Client
    /// </summary>
    internal sealed class StatusClient : Client {

        #region [Local Attributes]

        /// <summary>
        /// The Session this client is managing
        /// </summary>
        private IrcSession m_session;

        /// <summary>
        /// Channel Server, for multiple IO channels
        /// </summary>
        private ChannelServer m_server;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Status Client
        /// </summary>
        /// <param name="socket"></param>
        public StatusClient(Socket socket) : base(socket) {
            Console.WriteLine("New IRC Session {0}", ClientID.ToString());
            m_session = new IrcSession();

            m_session.Settings_Session[IrcSessionSettings.NICKNAME] = "mikeyj";
            m_session.Settings_Session[IrcSessionSettings.REAL_NAME] = "Mike Jamer";
            m_session.Settings_Session[IrcSessionSettings.USER_NAME] = "mike";

            m_server = new ChannelServer(); //Initialize Channel Server
            Console.WriteLine("Channel Server functional, accepting connections on port {0}.", m_server.Port);

            //Register event handling with irc session
            m_session.Event += onIrcEvent;


            return;
        }

        #endregion

        #region [Logic]

        /// <summary>
        /// Dispatch processes the message recieved
        /// </summary>
        /// <param name="response">Actual Response from the client</param>
        protected override void dispatch_client(string response) {
            if (response[0] == '!')
            {
                response = response.Substring(1);
                //TODO: Process it as a client command

                //Some commands will affect the main client, (the session server) or whatever,
                //While, others may affect this channel or well, anywyas, gonna figure this out...

                //in fact, we will have to have some type of event/notification system for the client itself, to 
                //allow it to properly react to events as they occur...

                if (response == "close")
                {
                    //TODO: close this channel
                }


            }
            else
            { //Otherwise, assume it was intended to go to the irc session
                m_session.send(response); 
            }


            return;
        }

        /// <summary>
        /// Act on IRC related events
        /// </summary>
        /// <param name="session">IRC Session</param>
        /// <param name="irc_event">Irc Event</param>
        private void dispatch_irc(IrcSession session, IrcEvent irc_event)
        {
            //TODO: Investigate how to keep this implementation relatively clean
            /**
             * It might be useful to implement the type of extension
             * system that we have in the moose core.
             */
            //this would technically allow us to echo status messages to the console...
            if (irc_event.EventType == IrcEventTypes.MessageStatus)
            {
                send(irc_event.Arguments[IrcEventArgumentType.Message].Value.ToString());
            }

            return;
        }

        #endregion

        #region [Event Handlers]

        /// <summary>
        /// Fires on IRC event
        /// </summary>
        /// <param name="session">The session affected by the event</param>
        /// <param name="irc_event">the actual irc event</param>
        private void onIrcEvent(IrcSession session, IrcEvent irc_event)
        {
            dispatch_irc(session, irc_event);
            return;
        }

        #endregion

    }
}
